<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1、2、3、before timeout、alse before timeout、4、test
        setTimeout(() => {
            // #1
            new Promise(resolve => {
                resolve();
            }).then(() => {
                // #6
                console.log('test');
            });

            console.log(4);
        });

        new Promise(resolve => {
            // 一旦调用了 resolve 就把后面的 then 对应的回调添加到了微任务队列
            resolve();
            console.log(1)
        }).then(() => {
            // #2
            console.log(3);
            // 执行 Promise.resolve() 又把后面的 then 添加到了微任务队列
            Promise.resolve().then(() => {
                // #3
                console.log('before timeout');
                // 函数默认返回 undefined
                // then 的函数其实返回默认的 Promise.resolve(undefined)
                // return Promise.resolve(undefined) // 隐藏！！！
            }).then(() => {
                // #4
                Promise.resolve().then(() => {
                    // #5
                    console.log('also before timeout')
                })
            })
        })
        console.log(2);
    </script>
</body>

</html>